<script>
export default {
  onLaunch: function() {
   
	// #ifdef MP-WEIXIN
	 this.getDeviceInfo();
	  this.update();
	// #endif
	
  },

  globalData: {
    version: "1.0.0",
    wrap_con:'', //可视区域高
  },
  data() {
    return {
      CALLBACK: res => {
        this.NetworkStatus(res.networkType);
      }
    };
  },

  onShow: function() {
  
  },

  computed: {},

  onHide: function() {},

  methods: {
    update(){
  // 获取小程序更新机制兼容
      if (wx.canIUse("getUpdateManager")) {
        const updateManager = wx.getUpdateManager();
        updateManager.onCheckForUpdate(function (res) {
          // 请求完新版本信息的回调
          if (res.hasUpdate) {
            updateManager.onUpdateReady(function () {
              wx.showModal({
                title: "更新提示",
                content: "新版本已经准备好，是否重启应用？",
                success: function (res) {
                  if (res.confirm) {
                    // 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
                    updateManager.applyUpdate();
                  }
                }
              });
            });
            updateManager.onUpdateFailed(function () {
              // 新的版本下载失败
              wx.showModal({
                title: "已经有新版本了哟~",
                content: "新版本已经上线啦~，请您删除当前小程序，重新搜索打开哟~"
              });
            });
          }
        });
      } else {
        // 如果希望用户在最新版本的客户端上体验您的小程序，可以这样子提示
        wx.showModal({
          title: "提示",
          content: "当前微信版本过低，无法使用该功能，请升级到最新微信版本后重试。"
        });
      }


    },
    getDeviceInfo() {
      const DeviceInfo = uni.getSystemInfoSync();
      this.$scope.globalData.wrap_con = 'height:'+(DeviceInfo.safeArea.bottom - 44 - 50 - DeviceInfo.statusBarHeight)+'px'
	  console.log(uni.getSystemInfoSync().platform)
	  
      uni.setStorageSync("DeviceInfo", DeviceInfo);
    },

    hadleNetWork() {
      let _this = this;

      uni.getNetworkType({
        success: function(res) {
          console.log(res.networkType);

          _this.NetworkStatus(res.networkType);
        }
      });
      uni.onNetworkStatusChange(_this.CALLBACK);
    }, //监听网络状态

    NetworkStatus(type) {
      this.SetNetworkType(type);

      if (type == "unknown" || type == "none" || type == "2g" || type == "3g") {
        this.SetIsnetwork(false);
      } else {
        this.SetIsnetwork(true);
      }
    },

    checkupdata() {
      let platform = uni.getSystemInfoSync().platform;

      let version = getApp().globalData.version;

      // console.log(version)

      let versiondot = version.replace(/\./g, "");

      let newversion = parseInt(versiondot);

      if (platform == "ios") {
        let params = {
          type: "2" //type 1:安卓  2：iso
        };

        this.$api.getAppVersion(params).then(res => {
          if (res.code === 0) {
            if (res.data) {
              console.log(res.data);

              let versionNumDot = res.data.appVersion.replace(/\./g, "");

              let newversionNum = parseInt(versionNumDot);

              if (newversion < newversionNum) {
                // uni.switchTab({
                // 	url: "pages/my-center/my-center"
                // })
                uni.showModal({
                  title: res.data.appVersion + "版本更新",

                  content:
                    res.data.description + "\n" + "请前往个人中心升级更新",

                  showCancel: false,

                  success: ee => {
                    if (ee.confirm) {
                      // plus.runtime.openURL(res.data.loadUrl);
                    }
                  }
                });
              }
            } else {
            }
          }
        });
      } else if (platform == "android") {
        console.log("我是安卓");

        let params = {
          type: "1" //type 1:安卓  2：iso
        };

        this.$api.getAppVersion(params).then(res => {
          // console.log(res);

          if (res.code === 0) {
            if (res.data) {
              let versionNumDot = res.data.appVersion.replace(/\./g, "");

              let newversionNum = parseInt(versionNumDot);

              if (newversion < newversionNum) {
                // uni.switchTab({
                // 	url: "pages/my-center/my-center"
                // })
                uni.showModal({
                  title: res.data.appVersion + "版本更新",

                  content:
                    res.data.description + "\n" + "请前往个人中心升级更新",

                  showCancel: false,

                  success: ee => {
                    if (ee.confirm) {
                      // plus.runtime.openURL(res.data.loadUrl);
                    }
                  }
                });
              }
            } else {
            }
          }
        });
      }
    },

    getFileUrl() {
      this.$api.getFileUrl().then(res => {
        if (res.code == 0) {
          uni.setStorageSync("fileUrl", res.data);
        }
      });
    },

    // 接受在线消息
    handlePushonLine() {
      // console.log("马上推送在线消息：")
      // uni.onPushMessage((res)=>{
      // 	console.log(res)
      // })
    },

    // 接受离线消息
    handlePushOff() {
      console.log("马上推送离线消息：");
      // #ifdef APP
      plus.push.setAutoNotification(true); //设置通知栏显示通知 //必须设置
      // #endif

      // #ifdef APP
      let _this = this;

      //点击在线通知后触发
      plus.push.addEventListener(
        "click",
        function(msg) {
          console.log("---------点击消息后触发------------");
          console.log(msg);

          // {
          // 	"__UUID__": "androidPushMsg196185135",
          // 	"title": "今日新闻",
          // 	"appid": "__UNI__47FF800",
          // 	"content": "吕梓睿是个大聪明",
          // 	"payload": {
          // 		"title": "今日新闻",
          // 		"content": "吕梓睿是个大聪明"
          // 	}
          // }

          // plus.runtime.setBadgeNumber(20)
          // plus.push.clear(); //清空通知栏

          _this.pushHandle(msg); //处理方法
        },
        false
      );

      //在线穿透触发
      plus.push.addEventListener(
        "receive",
        function(msg) {
          //allen 透传消息

          // if (plus.os.name == 'iOS') { //由于IOS 必须要创建本地消息 所以做这个判断

          // 	if (msg.payload && msg.payload != null && msg.type == 'receive') {

          // 		console.log(msg);
          // 		_this.pushHandle(msg)

          // 		// {"title": "xxx","content": "xxx","payload": "xxx"} 符合这种 才会自动创建消息  文档地址https://ask.dcloud.net.cn/article/35622

          // 		plus.push.createMessage(msg.payload.content, JSON.stringify(msg.payload)) //创建本地消息

          // 	}

          // }

          if (plus.os.name == "Android") {
            console.log("---------------------在线穿透触发-------------------");
            console.log(msg);

            // if (!msg.title || !msg.content || !msg.payload) { //  不符合自动创建消息的情况

            //这里根据你消息字段来创建消息

            plus.push.createMessage(msg.content, JSON.stringify(msg.payload)); //创建本地消息

            // } else {

            // 	//符合自动创建消息

            // 	_this.pushHandle(msg)

            // }
          }
        },
        false
      );

      // #endif
    },

    pushHandle(msg) {
      uni.showModal({
        title: msg.title,
        content: msg.content,
        success: function(res) {
          if (res.confirm) {
            console.log("用户点击确定");
          } else if (res.cancel) {
            console.log("用户点击取消");
          }
        }
      });

      if (typeof msg.payload == "string") {
        //如果是字符串，表示是ios创建的  要转换一下
        let payload = msg.payload.split("S.payload=")[1].split(";end")[0];
        console.log(payload);
        msg.payload = JSON.parse(payload);
        console.log(msg.payload);
      }

      if (!msg) return false;

      plus.runtime.setBadgeNumber(0); //清除app角标

      //下面的代码根据自己业务来写 这里可以写跳转业务代码

      //跳转到tab

      let item = msg.payload;

      if (item.type == 0 || item.type == 2 || item.type == 4) {
        uni.switchTab({
          url: "/pages/data/index"
        });

        uni.setStorageSync("newsType", item);
      }

      //跳转到详情

      if (item.type == 5) {
        console.log(111);

        if (item.contentType == 1) {
          let url = "/pages/index/article/articleDetails?articleId=" + item.id;

          uni.navigateTo({
            url: url
          });
        }

        if (item.contentType == 3) {
          let url =
            "/pages/index/videoDetails/videoDetails?articleId=" + item.id;

          uni.navigateTo({
            url: url
          });
        }
      }
    }
  }
};
</script>



<style lang="scss">
/*每个页面公共css */

page {
  font-size: 24rpx;
  font-family: PingFangSC-Semibold, PingFang SC;
  color: #303133;
  font-weight: 400;
}

@import "@/uni_modules/uview-ui/index.scss";

@import "@/static/style/common.min.css";

::-webkit-scrollbar {
  display: none;

  width: 0 !important;

  height: 0 !important;

  -webkit-appearance: none;

  background: transparent;
}
</style>
